package cn.lg.soar.system.auth.client;

import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.common.util.BeanCopier;
import cn.lg.soar.core.annotations.ApiClient;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.api.model.*;
import cn.lg.soar.system.api.service.IUserApi;
import cn.lg.soar.system.auth.entity.AccountBind;
import cn.lg.soar.system.auth.entity.User;
import cn.lg.soar.system.auth.service.IAccountBindService;
import cn.lg.soar.system.auth.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author luguoxiang
 * @date 2022/1/3
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@ApiIgnore
@ApiClient("boot")
@RestController
@RequestMapping(ModuleConst.CLIENT_PATH + "/user")
public class UserApiClient implements IUserApi {

    @Autowired
    private IUserService service;
    @Autowired
    private IAccountBindService accountBindService;

    private final static BeanCopier<User, UserQueryDTO> USER_TO_QUERYDTO = BeanCopier.create(User.class, UserQueryDTO.class);
    private final static BeanCopier<UserUpdateDTO, User> UPDATEDTO_TO_USER = BeanCopier.create(UserUpdateDTO.class, User.class);

    @GetMapping("listByIds")
    @Override
    public List<UserQueryDTO> listByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return USER_TO_QUERYDTO.convertBatch(service.listByIds(ids))
                .collect(Collectors.toList());
    }

    @GetMapping("getById")
    @Override
    public UserQueryDTO getById(@RequestParam Long userId) {
        return USER_TO_QUERYDTO.convertNullable(
                service.getById(userId)
        );
    }

    @GetMapping("getByAccount")
    @Override
    public UserQueryDTO getByAccount(Integer accountType, @RequestParam String account) {
        User user = accountBindService.getUser(accountType, account);
        return USER_TO_QUERYDTO.convertNullable(user);
    }

    @PostMapping("create")
    @Override
    public Long create(@RequestBody UserCreateDTO dto) {
        return service.create(dto);
    }

    @PostMapping("update")
    @Override
    public void update(@RequestBody UserUpdateDTO dto) {
        User user = UPDATEDTO_TO_USER.convert(dto);
        AssertUtil.isTrue(service.updateById(user), "更新用户失败");
    }

    @PostMapping("binding")
    @Override
    public void binding(@RequestBody UserBindDTO dto) {
        accountBindService.binding(dto.getAccountType(), dto.getAccount(), dto.getUserId());
    }

    @GetMapping("getUserCurrentAllRoleIds")
    @Override
    public List<Long> getUserCurrentAllRoleIds(@RequestParam Long userId) {
        return service.getUserCurrentAllRoleIds(userId);
    }

    @GetMapping("isSuperAdministrator/username")
    @Override
    public boolean isSuperAdministrator(@RequestParam String username) {
        return service.isSuperAdministrator(username);
    }

    @GetMapping("isSuperAdministrator/userId")
    @Override
    public boolean isSuperAdministrator(@RequestParam Long userId) {
        return service.isSuperAdministrator(userId);
    }

    @Override
    public List<AuthAccountBind> userAccountList(Long userId) {
        List<AccountBind> accounts = accountBindService.listByUserId(userId);
        return accounts.stream().map(x -> {
            AuthAccountBind authAccountBind = new AuthAccountBind();
            authAccountBind.setAccountType(x.getAccountType());
            authAccountBind.setAccount(x.getAccount());
            authAccountBind.setRemark(x.getRemark());
            return authAccountBind;
        }).collect(Collectors.toList());
    }
}
